package HeSuanServer.service.Impl;

import HeSuanServer.POJO.R.Result;
import HeSuanServer.POJO.UmSearch;
import HeSuanServer.POJO.UserMessage;
import HeSuanServer.mapper.UmMapper;
import HeSuanServer.service.UmService;
import HeSuanServer.uitl.StatusUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @描述:
 * @author: LH
 * @date: 2022/12/27
 */
@Slf4j
@Service
public class UmServiceImpl implements UmService {

    @Autowired
    private UmMapper umMapper;
    @Override
    public Result add(UserMessage message) {
        log.info("开始添加");
        Result R=new Result();
        try{
            umMapper.add(message);
            R.setStatusCode(200);
            R.setMsg("添加成功!");
        }catch (Exception e){
            e.printStackTrace();
            R.setStatusCode(500);
            R.setMsg("添加失败!");
        }
        log.info("添加结束");
        return R;
    }

    @Override
    public Result Search(UmSearch search) {
//        处理分页
        Integer currentPage = search.getCurrentPage();
        currentPage=(currentPage-1)*search.getPageSize();
        search.setCurrentPage(currentPage);
//        处理日期
        Date date = search.getDate();
        if(date!=null){
            Calendar instance = Calendar.getInstance();
            instance.setTime(date);
            instance.add(Calendar.DATE,1);
            Date time = instance.getTime();
            search.setEndDate(time);
        }
        Result R=new Result();
        Map map=new HashMap();
        try{
            List<UserMessage> list=umMapper.search(search);
            Integer total=umMapper.getTotal(search);
            List<UserMessage> list1 = StatusUtil.setStatus(list);
            map.put("list",list1);
            map.put("total",total);
            R.setStatusCode(200);
            R.setMsg("查询成功!");
        }catch (Exception e){
            e.printStackTrace();
            R.setMsg("查询失败");
            R.setData(500);
        }
        R.setData(map);
        return R;
    }

    @Override
    public Result getById(Integer id) {
        Result R=new Result();
        try {
            UserMessage message=umMapper.getById(id);
            UserMessage message1 = StatusUtil.setR(message);
            R.setData(message1);
            R.setMsg("根据id查询成功!");
            R.setStatusCode(200);
        }catch (Exception e){
            e.printStackTrace();
            R.setMsg("根据id查询失败");
            R.setStatusCode(500);
        }
        return R;
    }

    @Override
    public Result edit(UserMessage message) {
        Result R=new Result();
        switch (message.getResult()){
            case "检测中": message.setResult("0");break;
            case "阳性": message.setResult("2");break;
            case "阴性": message.setResult("1");break;
        }
        try {
            umMapper.edit(message);
            R.setStatusCode(200);
            R.setMsg("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            R.setStatusCode(500);
            R.setMsg("修改失败");
        }
        return R;
    }

    @Override
    public Result del(Integer id) {
        Result R=new Result();
        try {
            umMapper.del(id);
            R.setMsg("删除成功!");
            R.setStatusCode(200);
        }catch (Exception e){
            e.printStackTrace();
            R.setStatusCode(500);
            R.setMsg("删除失败!");
        }
        return R;
    }
}
